<template>
	<view class="content" :style="'padding-top:' + (90 + parseInt(+statusbarHeight)) + 'rpx'">
		<view class="navigation" :style="'padding-top:'+ statusbarHeight + 'rpx'">
			<image v-if="path" @click="pageBack" class="back_icon" src="/static/images/to_left.png" mode="scaleToFill"></image>
			<text>行程申请</text>
		</view>
		<view class="date_box">
			<view class="date_change" @click="showDate = true">
				<!-- <custom-year-month-selector ref="selector" @change="dateChange"></custom-year-month-selector> -->
				<!-- <image class="down_icon" src="/static/images/down.png" mode="scaleToFill"></image> -->
				<slot>{{dateValue.textCN}}</slot>
				<image class="down_icon" src="../../static/images/down.png" mode="scaleToFill"></image>
			</view>
			<view class="apply_times">
				{{dateValue.value[1]}}月{{current==0?'待':'已'}}批申请共{{jiaList.length}}条
			</view>
		</view>
		<view class="apply_list">
			<view class="apply_item" v-for="(item, index) in jiaList" :key="index" @click="jumpToDetail(item)">
				<custom-card>
					<view class="apply_header">
						<view class="apply_type">
							<text>{{item.empName}}提交的{{item.leaveType}}申请</text>
						</view>
						<view class="apply_result"
							:class="item.leaveResult == 0?'appling':item.leaveResult == 1?'agree':'not_agree'">
							<image class="result_icon"
								:src="item.leaveResult == 0?'/static/images/appling.png':item.leaveResult == 1?'../../static/images/agree.png':'../../static/images/not_agree.png'"
								mode="scaleToFill"></image>
							<text>{{item.leaveResult == 0?'审批中':item.leaveResult == 1?'审批通过':'审批拒绝'}}</text>
						</view>
					</view>
					<view class="apply_start_and">
						{{item.leaveStartDate}} 至 {{item.leaveEndDate}}&nbsp;{{calculate(item.leaveStartTime, item.leaveEndTime)}}
					</view>
					<view class="apply_date">
						申请时间：{{item.fillTime}}
					</view>
				</custom-card>
			</view>
		</view>
		<custom-year-month-selector-plus v-model="showDate" @change="dateChange" />
		<view class="segmented">
			<uni-segmented-control :current="current" :values="['待批申请','已批申请']" @clickItem="_onClickItem" styleType="text"></uni-segmented-control>
		</view>
	</view>
</template>

<script>
	let _this;
	import {
		getTodoLeaveList,
		getInvolvedLeaveList,
	} from '../../common/api/index.js';
	import customYearMonthSelector from '@/components/custom-year-month-selector/custom-year-month-selector'
	import customYearMonthSelectorPlus from '@/components/custom-year-month-selector/custom-year-month-selector-plus'
	import {
		_showLoading,
		_showToast,
		dateFtt,
		mGetDate,
		datediff
	} from '../../common/js/util.js'
	export default {
		components: {
			customYearMonthSelector,
			customYearMonthSelectorPlus
		},
		data() {
			return {
				statusbarHeight: 45,
				showDate: false,
				applyType: 3,
				orgId: null,
				empId: null,
				path: null,
				dateValue: {},
				scrollTop: 0,
				height: 0,
				current: 0,
				jiaList: [],
				askTypes: [{
					text: '请选择',
					value: '',
				}, {
					text: '事假',
					value: '1',
				}, {
					text: '病假',
					value: '2',
				}, {
					text: '年假',
					value: '3',
				}, {
					text: '婚假',
					value: '4',
				}, {
					text: '产检假',
					value: '5',
				}, {
					text: '产假',
					value: '6',
				}, {
					text: '陪产假',
					value: '7',
				}, {
					text: '丧假',
					value: '8',
				}, {
					text: '调休',
					value: '9',
				}, {
					text: '奖励假',
					value: '10',
				}, {
					text: '实习',
					value: '11',
				}, {
					text: '实习返校 ',
					value: '12',
				}, {
					text: '外出条 ',
					value: '13',
				}],
			}
		},
		onShow() {
			setTimeout(()=>{
				this._getData()
			},300)
		},
		onLoad(option) {
			_this = this
			this.statusbarHeight = uni.getStorageSync('SET_STATUS_BAR') * 2
			this.orgId = option.orgId
			this.empId = option.empId
			this.path = option.path?true:false
			this.applyType = option.applyType
			//TODO:修改兼容性，不使用$refs
			let value = this.getDate().split('-');
			let textCN =  value[0] + '年' + value[1] + '月'
			this.dateValue = {
				value,
				textCN,
				sAndeforTime: [dateFtt('yyyy-MM-dd', value[0] + '-' + value[1] + '-01') + ' 00:00:00', dateFtt(
						'yyyy-MM-dd', value[0] + '-' + value[1] + '-' + mGetDate(value[0], value[1])) +
					' 23:59:59'],
			}
			// this._getData()
		},
		methods: {
			jumpToDetail(item) {
				uni.navigateTo({
					url: `./askForLeaveDetails?detail=${encodeURIComponent(JSON.stringify(item).replace(/<[^>]+>/g, ""))}&orgId=${this.orgId}&empId=${this.empId}`
				})
			},
			_getData(){
				// if(_this.loading) return
				uni.showLoading({
					mask: true,
					title: '加载中...',
				})
				if (_this.current === 0){
					_this._getTodoLeaveList()
				} else {
					_this._getInvolvedLeaveList()
				}
			},
			dateChange(e) {
				this.dateValue = e
				this._getData()
			},
			_onClickItem(e){
				_this.current = e.currentIndex
				this._getData()
			},
			_getTodoLeaveList() {
				this.jiaList = []
				getTodoLeaveList({
					orgId: this.orgId,
					empId: this.empId,
					startTime: this.dateValue.sAndeforTime[0],
					endTime: this.dateValue.sAndeforTime[1],
				}).then(res => {
					if (res.resultCode == 0) {
						uni.hideLoading()
						if (res.leaveList != null && res.leaveList.length > 0){
							res.leaveList.forEach(item => {
								item['leaveEndDate'] = dateFtt('yyyy-MM-dd', item.leaveEndTime)
								item['leaveStartDate'] = dateFtt('yyyy-MM-dd', item.leaveStartTime)
								item.leaveType = this.checkType(item.leaveType)
								this.jiaList.push(item)
							})
						}else {
							// uni.showToast({
							// 	icon: 'error',
							// 	title: '暂无数据',
							// })
						}
					} else {
						uni.showToast({
							icon: 'error',
							title: res.resultMsg,
						})
					}
				})
			},
			_getInvolvedLeaveList(){
				this.jiaList = []
				getInvolvedLeaveList({
					orgId: this.orgId,
					empId: this.empId,
					startTime: this.dateValue.sAndeforTime[0],
					endTime: this.dateValue.sAndeforTime[1],
				}).then(res => {
					if (res.resultCode == 0) {
						uni.hideLoading()
						if (res.leaveList != null && res.leaveList.length > 0){
							res.leaveList.forEach(item => {
								item['leaveEndDate'] = dateFtt('yyyy-MM-dd', item.leaveEndTime)
								item['leaveStartDate'] = dateFtt('yyyy-MM-dd', item.leaveStartTime)
								item.leaveType = this.checkType(item.leaveType)
								this.jiaList.push(item)
							})
						}else {
							// uni.showToast({
							// 	icon: 'error',
							// 	title: '暂无数据',
							// })
						}
					} else {
						uni.showToast({
							icon: 'error',
							title: res.resultMsg,
						})
					}
				})
			},
			checkType(type) {
				const checkType = this.askTypes.filter(item => item.value == type)
				return checkType[0].text
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				month = month > 9 ? month : '0' + month;;
				return `${year}-${month}`;
			},
			pageBack() {
				uni.navigateBack({
					delta: 1
				})
			}
		},
		computed: {
			calculate(start, end) {
				return function(start, end) {
					start = start.replace(/-/g, "/")
					end = end.replace(/-/g, "/")
					let diff = datediff(start, end)
					let jiaNum = diff % 1 * 24 >= 1 ? (diff % 1 * 24 >= 6 ? (Number.parseInt(diff) + 1) : (Number.parseInt(
					diff) + 0.5)) : Number.parseInt(diff)
					
					if (diff > 1) {
						return jiaNum + "天（" + Number.parseFloat(diff).toFixed(2) + "天）"
					}
					diff = diff * 24
					if (diff > 1) {
						return jiaNum + "天（" + Number.parseFloat(diff).toFixed(2) + "小时）"
					}
					diff = diff * 60
					if (diff > 1) {
						return Number.parseFloat(diff).toFixed(2) + "分钟"
					}
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100vh;
		background-color: #f6f6f6;
		font-family: PingFang-SC-Heavy, PingFang-SC;
	}

	.content {
		height: 100%;
		padding-bottom: 120rpx;
		box-sizing: border-box;
		overflow-y: auto;
		// position: relative;
		// z-index: 999;
		/* padding: 28rpx 32rpx; */
	}
	
	.navigation {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: row;
		height: 90rpx;
		position: fixed;
		background-color: #FFF;
		top: 0;
		left: 0;
		right: 0;
		color: #333;
		z-index: 9999;
	}
	
	.back_icon {
		width: 48rpx;
		height: 48rpx;
		position: absolute;
		left: 12rpx;
	}
	
	.segmented{
		padding: 24rpx;
		background-color: #FFF;
		height: 120rpx;
		box-sizing: border-box;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 28;
		box-shadow: 0 16rpx 32rpx 0 rgba(166, 162, 176, 0.2);
	}
	.btn_box {
		height: 150rpx;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		border-radius: 24rpx 24rpx 0 0;
		background-color: #FFF;
	}

	.btn_pri {
		width: 566rpx;
		height: 96rpx;
		line-height: 96rpx;
		font-weight: 800;
		background: linear-gradient(90deg, #0685FF 0%, #285EFF 100%);
		box-shadow: 0 16rpx 32rpx 0 rgba(36, 98, 255, 0.24);
		border-radius: 9rpx;
	}

	.date_box {
		padding: 32rpx;
	}

	.date_change {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		font-size: 30rpx;
		font-weight: 800;
		color: #3C424F;
		line-height: 42rpx;
	}

	.down_icon {
		width: 48rpx;
		height: 48rpx;
	}

	.apply_times {
		margin-top: 16rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #6B707A;
		line-height: 40rpx;
	}

	.apply_list {
		padding: 0 32rpx;

		.apply_item {
			margin-bottom: 20rpx;
		}
	}

	.apply_header {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		position: relative;

		// &::before {
		// 	content: "";
		// 	display: block;
		// 	position: absolute;
		// 	width: 10rpx;
		// 	height: 10rpx;
		// 	border-radius: 50%;
		// 	background-color: #185AFF;
		// 	left: -16rpx;
		// }

		.apply_type {
			font-size: 32rpx;
			font-weight: 600;
			color: #3C424F;
			line-height: 44rpx;
		}

		.apply_result {
			display: flex;
			flex-direction: row;
			align-items: center;
			font-size: 28rpx;
			font-weight: 500;
			color: #FA6400;
			line-height: 40rpx;
		}

		.result_icon {
			width: 36rpx;
			height: 36rpx;
			margin-right: 12rpx;
		}

		.apply_result.agree {
			color: #55C976;
		}

		.apply_result.not_agree {
			color: #E02020;
		}

		.apply_result.appling {
			color: #FA6400;
		}
	}

	.apply_start_and {
		font-size: 28rpx;
		font-weight: 600;
		color: #6B707A;
		line-height: 40rpx;
		padding: 20rpx 0 30rpx;
	}

	.apply_date {
		font-size: 24rpx;
		font-weight: 500;
		color: #9AA2B2;
		line-height: 34rpx;
	}
</style>
